package org.dromara.book.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.dromara.book.constant.CommonConstantUtils;
import org.dromara.book.domain.vo.SzlyBaseIsbnVo;

import java.util.*;
import java.util.stream.Collectors;

public class ISBNUtil {

    public static List<SzlyBaseIsbnVo> queryAliyun(String isbn){
        HttpRequest post = HttpUtil.createPost(CommonConstantUtils.ISBN_HOST);
        Map<String, String> headers = new HashMap<>();
        // 最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + CommonConstantUtils.APPCODE);
        // 根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        Map<String, Object> bodys = new HashMap<>();
        bodys.put("isbn", isbn);
        post.addHeaders(headers);
        HttpResponse execute = post.form(bodys).execute();
        if(HttpStatus.HTTP_OK==execute.getStatus()){
            String body = execute.body();
            List<SzlyBaseIsbnVo> collect = JSONUtil.parseObj(body).getJSONObject("data").getJSONArray("details").stream().map(i -> {
                JSONObject jsonObject = JSONUtil.parseObj(i);
                SzlyBaseIsbnVo szlyBaseIsbnVo = new SzlyBaseIsbnVo();
                szlyBaseIsbnVo.setApiResult(jsonObject);
                szlyBaseIsbnVo.setIsbn13(jsonObject.getStr("isbn"));
                szlyBaseIsbnVo.setName(jsonObject.getStr("title"));
                szlyBaseIsbnVo.setFirstAuthor(jsonObject.getStr("author"));
                szlyBaseIsbnVo.setPrice(jsonObject.getBigDecimal("price"));
                szlyBaseIsbnVo.setPublishCompany(jsonObject.getStr("publisher"));
                szlyBaseIsbnVo.setPublishPlace(jsonObject.getStr("pubPlace"));
                szlyBaseIsbnVo.setPublishDept(jsonObject.getStr("publishDept"));
                szlyBaseIsbnVo.setPublishDate(jsonObject.getStr("pubDate"));
                szlyBaseIsbnVo.setPagesNum(jsonObject.getInt("page"));
                szlyBaseIsbnVo.setBind(jsonObject.getStr("binding"));
                szlyBaseIsbnVo.setBookVersion(jsonObject.getStr("edition"));
                szlyBaseIsbnVo.setBookFormat(jsonObject.getStr("format"));
                szlyBaseIsbnVo.setKeyword(jsonObject.getStr("keyword"));
                szlyBaseIsbnVo.setLanguageCode(jsonObject.getStr("language"));
                szlyBaseIsbnVo.setBookIntroduction(jsonObject.getStr("gist"));
                szlyBaseIsbnVo.setCoverImage(jsonObject.getStr("img"));
                szlyBaseIsbnVo.setIsbn10(jsonObject.getStr("isbn10"));
                szlyBaseIsbnVo.setTypeCode(jsonObject.getStr("genus"));
                szlyBaseIsbnVo.setCip(jsonObject.getStr("cipTxt"));
                szlyBaseIsbnVo.setWordsNum(jsonObject.getInt("wordNum"));
                szlyBaseIsbnVo.setGeneralNotes(jsonObject.getStr("annotation"));
                szlyBaseIsbnVo.setGeneralNotes(jsonObject.getStr("annotation"));
                szlyBaseIsbnVo.setGeneralNotes(jsonObject.getStr("annotation"));
                szlyBaseIsbnVo.setGeneralNotes(jsonObject.getStr("annotation"));
                szlyBaseIsbnVo.setGeneralNotes(jsonObject.getStr("annotation"));
                return szlyBaseIsbnVo;
            }).collect(Collectors.toList());
            return collect;
        }
        return new ArrayList<>();
    }



}
